﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace DBProvider
{
	public class CommonHelper
	{
		public static Decimal ConvertToDecimal(string theDecimalValue)
		{
			if(string.IsNullOrEmpty(theDecimalValue))
			{
				return Decimal.Zero;
			}

			Decimal result = new Decimal();

			if(Decimal.TryParse(theDecimalValue, out result))
			{
				return result;
			}
			else
			{
				return Decimal.Zero;
			}
		}

		public static int ConvertToInt(string theValue)
		{
			if(string.IsNullOrEmpty(theValue))
			{
				return int.MinValue;
			}

			int result = 0;

			int.TryParse(theValue, out result);

			return result;
		}

		public static bool ConvertToBool(string theValue)
		{
			if(string.IsNullOrEmpty(theValue))
			{
				return false;
			}

			bool result = false;

			bool.TryParse(theValue, out result);

			return result;
		}

		public static DateTime ConvertToDatetime(string theDatetime)
		{
			if(string.IsNullOrEmpty(theDatetime))
			{
				return DateTime.MinValue;
			}

			DateTime result = new DateTime();

			if(DateTime.TryParse(theDatetime, out result))
			{
				return result;
			}

			string[] dateAndTime = theDatetime.Split('T');

			if(dateAndTime != null && dateAndTime.Length == 2)
			{
				int year = 0;
				int month = 0;
				int day = 0;
				int hour = 0;
				int minute = 0;
				int second = 0;

				if(!string.IsNullOrEmpty(dateAndTime[0]))
				{
					int.TryParse(dateAndTime[0].Substring(0, 4), out year);
					int.TryParse(dateAndTime[0].Substring(4, 2), out month);
					int.TryParse(dateAndTime[0].Substring(6, 2), out day);
				}

				if(!string.IsNullOrEmpty(dateAndTime[1]))
				{
					int.TryParse(dateAndTime[1].Substring(0, 2), out hour);
					int.TryParse(dateAndTime[1].Substring(2, 2), out minute);
					int.TryParse(dateAndTime[1].Substring(4, 2), out second);
				}

				return new DateTime(year, month, day, hour, minute, second);
			}

			if(theDatetime.Length == 8)
			{
				int year = 0;
				int month = 0;
				int day = 0;

				int.TryParse(theDatetime.Substring(0, 4), out year);
				int.TryParse(theDatetime.Substring(4, 2), out month);
				int.TryParse(theDatetime.Substring(6, 2), out day);

				return new DateTime(year, month, day);
			}

			return DateTime.MinValue;

		}

		// The Initialization Vector for the DES encryption routine
		private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

		/// <summary>
		/// Encrypts provided string parameter
		/// </summary>
		public static string Encrypt(string s)
		{
			if(string.IsNullOrEmpty(s))
			{
				return string.Empty;
			}

			string result = string.Empty;

			try
			{
				byte[] buffer = Encoding.ASCII.GetBytes(s);

				TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

				MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();

				des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("cryptoKey"));
				des.IV = IV;

				result = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length));
			}
			catch
			{
				throw;
			}

			return result;
		}

		/// <summary>
		/// Decrypts provided string parameter
		/// </summary>
		public static string Decrypt(string s)
		{
			if(string.IsNullOrEmpty(s))
			{
				return string.Empty;
			}

			string result = string.Empty;

			try
			{
				byte[] buffer = Convert.FromBase64String(s);

				TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();

				MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();

				des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes("cryptoKey"));

				des.IV = IV;

				result = Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length));
			}
			catch
			{
				throw;
			}

			return result;
		}

		public static bool ValidationIPAddress(string ip)
		{
			if(string.IsNullOrEmpty(ip))
			{
				return false;
			}

			System.Net.IPAddress address;

			return System.Net.IPAddress.TryParse(ip, out address);
		}
	}
}
